﻿#include "userdao.h"
#include <QSqlError>
#include <QDebug>


UserDao::UserDao(QSqlDatabase sqlDatabase)
{
    db = sqlDatabase;
}

bool UserDao::getTotalByParam(uint64_t &total)
{
    bool res = false;
    User user;
    QSqlQuery sql_query(db);
    QString sqlStr = QString("SELECT COUNT(*) FROM %1").arg(user.getTableName());
    if (sql_query.exec(sqlStr))
    {
        if (sql_query.next())
        {
            res = true;
            total = sql_query.value(0).toULongLong();
        }
        else
        {
            qCritical() << __FUNCTION__ << "not find COUNT!" << sqlStr;
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool UserDao::getPageListByParam(QList<User> &list,const  uint64_t &page,const  uint64_t &pageSize)
{
    bool res = false;
    User userTmp;
    QSqlQuery sql_query(db);
    uint64_t offset = (page - 1) * pageSize;
    QString sqlStr = QString("%1 LIMIT :limitVal OFFSET :offsetVal").arg(userTmp.generateSelectSql());
    sql_query.prepare(sqlStr);
    sql_query.bindValue(":limitVal", pageSize);
    sql_query.bindValue(":offsetVal", offset);
    if (sql_query.exec())
    {
        res = true;
        while (sql_query.next())
        {
            userTmp.clear();
            userTmp.fromSqlQuery(sql_query);
            qDebug() << "a4" << userTmp.toJsonObject();
            list.append(userTmp);
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool UserDao::getOne(User &object)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateSelectSql();
    if (sql_query.exec(sqlStr))
    {
        res = true;
        if(sql_query.next()){
            object.fromSqlQuery(sql_query);
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool UserDao::saveOne(User &object)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateInsertSql();
    if(object.getId() != 0){
        sqlStr = object.generateUpdateSql();
    }
    qDebug()<<__FUNCTION__<<sqlStr;
    if (sql_query.exec(sqlStr))
    {
        res = true;
        //如果是修改直接跳出
        if(object.getId() != 0) return res;

        sqlStr = QString("%1 where username = '%2'").arg(object.generateSelectSql()).arg(object.getUsername());
        if (sql_query.exec(sqlStr))
        {
            if (sql_query.next())
            {
                object.setId(sql_query.value("id").toULongLong());
            }
            else
            {
                qCritical() << __FUNCTION__ << "not find by username!" << sqlStr;
            }
        }
        else
        {
            qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool UserDao::deleteOne(User &object)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateDeleteSql();
    qDebug() << object.generateDeleteSql();
    if (sql_query.exec(sqlStr))
    {
        res = true;
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}
